<?php
/**
小区活动
 */
namespace App\Http\Controllers\Property;

use App\Common\json;
use App\Http\Controllers\Controller;
use App\Libraries\upload;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ActivityController extends Controller{

    //获取点击报名
    public function applylike($release_id){
        $data = DB::table('apply')
            ->leftjoin('user','apply.apply_user','=','user.user_id')
            ->where('apply_activity',$release_id)
            ->get();
        if($data == true){
            foreach ($data as $value){
                $id   = $value ->user_id;
                $heads = $value->head;
                $user_id = $value->nickname;
                $time    = $value ->apply_time;
                $lk[] = array('user_id'=>$id,'head' => $heads, 'nickname' => $user_id,'time'=>$time);
            }
            return $lk;
        }else{
            return [];
        }
    }

    //报名与取消报名
    public function apply(Request $request){
        if($request ->isMethod('post')){
            $activity_id          = $request ->input('activity_id');
            $user_id              = $request ->input('user_id');
            $select        = DB::table('apply')
                ->where('apply_user','=',$user_id)
                ->where('apply_activity','=',$activity_id)
                ->first();
            if($select  == true){
                $delete  =   DB::table('apply') ->where('apply_user','=',$user_id)
                    ->where('apply_activity','=',$activity_id) ->delete();
                if($delete == true){
                    return json::json('success','取消报名',json::value(1));
                }else{
                    return json::json('failure','由于网络或者不正当原因取消报名失败',json::value(1));
                }
            }else{
                $insert    = DB::table('apply')->insert([
                    'apply_user'           => $user_id,
                    'apply_activity'        => $activity_id,
                    'apply_time'             => date('Y-m-d H:i:s')
                ]);
                if($insert  == true){
                    return json::json('success','报名成功',json::value(1));
                }else{
                    return json::json('failure','由于网络或者不正当原因报名失败',json::value(1));
                }
            }

        }
    }

    public function applysum($activity_id){
        $select        = DB::table('apply')
            ->where('apply_activity',$activity_id)
            ->count();
        return $select;
    }

    public function Respay($activity_id,$user_id){
        $select        = DB::table('apply')
            ->where('apply_user',$user_id)
            ->where('apply_activity','=',$activity_id)
            ->get();
        if($select == true){
            $data   =1;
        }else{
            $data   =0;
        }
        return $data;
    }

    //获取全部活动信息
    public function AllActivity(Request $request,$type){
        if($request ->isMethod('post')){
            $village_id       = $request ->input('village_id');
            $time= date('Y-m-d H:i:s',time());
            if($type ==1){
                $user   = DB::table('user')
                    ->select('activity_id','head','nickname','activity_create_time','headline','site','start_time','end_time','attend','activity_isofficial')
                    ->join('activity','user.user_id','=','activity.user_activity')
                    ->where('end_time','<',$time)
                    ->where('activity_isofficial','1')
                    ->where('user_village',$village_id)
                    ->get();
                $work   = DB::table('work')
                    ->select('activity_id','work_head','village_name','activity_create_time','headline','site','start_time','end_time','attend','activity_isofficial')
                    ->join('activity','work.work_village','=','activity.activity_village')
                    ->join('village','work.work_village','=','village.village_id')
                    ->where('end_time','<',$time)
                    ->where('activity_isofficial','2')
                    ->where('work_village',$village_id)
                    ->get();
                if(! empty($user) && ! empty($work)) {
                    $data = array_merge($user, $work);
                }elseif (! empty($user)){
                    $data = $user;
                }elseif (! empty($work)){
                    $data  = $work;
                }else{
                    $data  = [];
                }
                foreach ($data as $value){
                    if($value ->activity_isofficial ==1){
                        $head    = $value ->head;
                        $nickname  = $value ->nickname;
                    }else{
                        $head    = $value ->work_head;
                        $nickname =$value->village_name;
                    }
                    $id            = $value ->activity_id;
                    $sum           = $this->applysum($id);
                    $dd[]          = array(
                        'activity_id'=>$value->activity_id,
                        'head'        =>$head,
                        'nickname'    =>$nickname,
                        'activity_create_time'    =>$value ->activity_create_time,
                        'headline'    =>$value ->headline,
                        'site'    =>$value ->site,
                        'start_time'    =>$value ->start_time,
                        'end_time'    =>$value ->end_time,
                        'attend'    =>$value ->attend,
                        'sum'       =>$sum,
                        'activity_isofficial' =>$value ->activity_isofficial
                    );
                }
                if(! empty($dd)) {
                    foreach ($dd as $k => $v) {
                        $times[$k] = $v['activity_create_time'];
                        $release_id[$k] = $v['activity_id'];
                    }
                    array_multisort($release_id, SORT_DESC, $times, SORT_ASC, $dd);
                }else{
                    $dd  = [];
                }
                if ($dd == true) {
                    return json::json('success', '获取正在进行中活动成功', $dd);
                } else {
                    return json::json('failure', '获取正在进行中失败', json::value(2));
                }
                return json::json('failure', '获取失败', json::value(2));
            }else{
                $user   = DB::table('user')
                    ->select('activity_id','head','nickname','activity_create_time','headline','site','start_time','end_time','attend','activity_isofficial')
                    ->join('activity','user.user_id','=','activity.user_activity')
                    ->where('end_time','>',$time)
                    ->where('activity_isofficial','1')
                    ->where('user_village',$village_id)
                    ->get();
                $work   = DB::table('work')
                    ->select('activity_id','work_head','village_name','activity_create_time','headline','site','start_time','end_time','attend','activity_isofficial')
                    ->join('activity','work.work_village','=','activity.activity_village')
                    ->join('village','work.work_village','=','village.village_id')
                    ->where('end_time','>',$time)
                    ->where('activity_isofficial','2')
                    ->where('work_village',$village_id)
                    ->get();
                if(! empty($user) && ! empty($work)) {
                    $data = array_merge($user, $work);
                }elseif (! empty($user)){
                    $data = $user;
                }elseif (! empty($work)){
                    $data  = $work;
                }else{
                    $data  = [];
                }
                foreach ($data as $value){
                    if($value ->activity_isofficial ==1){
                        $head    = $value ->head;
                        $nickname  = $value ->nickname;
                    }else{
                        $head    = $value ->work_head;
                        $nickname =$value->village_name;
                    }
                    $id            = $value ->activity_id;
                    $sum           = $this->applysum($id);
                    $dd[]          = array(
                        'activity_id'=>$value->activity_id,
                        'head'        =>$head,
                        'nickname'    =>$nickname,
                        'activity_create_time'    =>$value ->activity_create_time,
                        'headline'    =>$value ->headline,
                        'site'    =>$value ->site,
                        'start_time'    =>$value ->start_time,
                        'end_time'    =>$value ->end_time,
                        'attend'    =>$value ->attend,
                        'sum'       =>$sum,
                        'activity_isofficial' =>$value ->activity_isofficial
                    );
                }
                if(! empty($dd)) {
                    foreach ($dd as $k => $v) {
                        $times[$k] = $v['activity_create_time'];
                        $release_id[$k] = $v['activity_id'];
                    }
                    array_multisort($release_id, SORT_DESC, $times, SORT_ASC, $dd);
                }else{
                    $dd  = [];
                }
                if ($dd == true) {
                    return json::json('success', '获取结束的活动成功', $dd);
                } else {
                    return json::json('failure', '获取结束的活动失败', json::value(2));
                }
                return json::json('failure', '获取失败', json::value(2));
            }
        }else{
            return json::json('101','提交方式错误','');
        }
    }

   //获取自己的发布的活动信息
    public function SelectActivity(Request $request){
        if($request ->isMethod('post')){
            $user_id    = $request ->input('user_id');
            $data       = DB::table('activity')
                        ->where('user_activity',$user_id)
                        ->get();
            foreach ($data as $key =>$value){
                $activity_id     = $value ->activity_id;
                $headline        = $value ->headline;
                $site            = $value ->site;
                $start_time      = $value ->start_time;
                $end_time        = $value ->end_time;
                $describe        = $value ->describe;
                if(empty($value->activity_image)){
                    $activity_image  = "";
                }else {
                    $activity_image = explode(',', $value->activity_image);
                }
                $da[$key]['activity_id'] =$activity_id;
                $da[$key]['headline'] =$headline;
                $da[$key]['site'] =$site;
                $da[$key]['start_time'] =$start_time;
                $da[$key]['end_time'] =$end_time;
                $da[$key]['describe'] =$describe;
                $da[$key]['activity_image'] =$activity_image;
            }
            if ($data == true) {
                return json::json('success', '获取自己发布的小区活动成功', $da);
            } else {
                return json::json('failure', '获取自己发布的小区活动失败', json::value(2));
            }
        }else{
            return json::json('101','提交方式错误','');
        }
    }


    //获取信息
    public function particularsActivity(Request $request){
        if($request ->isMethod('post')){
            $user_id     = $request ->input('activity_id');
            $id          = $request ->input('user_id');
            $data        = DB::table('activity')

                         ->where('activity_id',$user_id)
                         ->get();
            $Respay      = $this ->Respay($user_id,$id);
            $sum         = $this ->applysum($user_id);
            $list        = $this ->applylike($user_id);
            foreach ($data as $key =>$value){
                $userid          = $value ->user_activity;
                $activity_id     = $value ->activity_id;
                $headline        = $value ->headline;
                $site            = $value ->site;
                $start_time      = $value ->start_time;
                $end_time        = $value ->end_time;
                $describe        = $value ->describe;
                if(empty($value->activity_image)){
                    $activity_image  = [];
                }else {
                    $activity_image = explode(',', $value->activity_image);
                }
                //$da['activity_image'] =$activity_image;
                $das  = array('user_id'=>$userid,'activity_id' => $activity_id,'headline' =>$headline,'site'=>$site,'start_time'=>$start_time,
                    'end_time' =>$end_time,'describe'=>$describe,'activity_image' =>$activity_image,'sum'=>$sum,
                    'apply' =>$Respay,
                    'list'=>$list
                );
            }
            if ($data == true) {
                return json::json('success', '获取信息成功', $das);
            } else {
                return json::json('failure', '获取信息失败', json::value(1));
            }
        }else{
            return json::json('101','提交方式错误','');
        }
    }


    //发布小区活动
    public function AddActivity(Request $request){
        if($request ->isMethod('post')){
            $user_id     = $request ->input('user_id');
           // $village_id  = $request ->input('village_id');
            $headline    = $request ->input('headline');
            $site        = $request ->input('site');
            $start_time  = $request ->input('start_time');
            $end_time    = $request ->input('end_time');
            $describe    = $request ->input('describe');
            $activity_image = $request ->input('activity_image');
            $time           = date("Y-m-d H:i:s");
            $data           = DB::table('activity')
                            ->insert([
                                'user_activity'   => $user_id,
                              //  'activity_village' =>$village_id,
                                'headline'         => $headline,
                                'site'             => $site,
                                'start_time'      => $start_time,
                                'end_time'        => $end_time,
                                'describe'        => $describe,
                                'activity_image' => $activity_image,
                                'activity_create_time' =>$time
                            ]);
            if ($data == true) {
                return json::json('success', '发布小区活动成功', json::value(1));
            } else {
                return json::json('failure', '发布小区活动失败', json::value(1));
            }
        }else{
            return json::json('101','提交方式错误','');
        }
    }
    public function UpdateActivity(Request $request){
        if($request ->isMethod('post')){
            $user_id     = $request ->input('activity_id');
            $headline    = $request ->input('headline');
            $site        = $request ->input('site');
            $start_time  = $request ->input('start_time');
            $end_time    = $request ->input('end_time');
            $describe    = $request ->input('describe');
            $activity_image = $request ->input('activity_image');
            $data           = DB::table('activity')
                ->where('activity_id',$user_id)
                ->update([
                    'headline'         => $headline,
                    'site'             => $site,
                    'start_time'      => $start_time,
                    'end_time'        => $end_time,
                    'describe'        => $describe,
                    'activity_image' => $activity_image
                ]);
            if ($data == true) {
                return json::json('success', '修改小区活动成功', json::value(1));
            } else {
                return json::json('failure', '修改小区活动失败', json::value(1));
            }
        }else{
            return json::json('101','提交方式错误','');
        }
    }

    public function DelActivity(Request $request){
        if($request ->isMethod('post')){
            $user_id     = $request ->input('activity_id');
            $data           = DB::table('activity')
                             ->where('activity_id',$user_id)
                             ->delete();
            if ($data == true) {
                return json::json('success', '删除小区活动成功', json::value(1));
            } else {
                return json::json('failure', '删除小区活动失败', json::value(1));
            }
        }else{
            return json::json('101','提交方式错误','');
        }
    }


    public function ActivityLike(Request $request){
        if($request ->isMethod('post')){
            $user_id       = $request ->input('user_id');
            $activity_id   = $request ->input('activity_id');
            $select        = DB::table('activitylike')
                           ->where('user_activitylike',$user_id)
                           ->where('activity_activitylike',$activity_id)
                           ->get();
            if($select == true){
                return json::json('success', '点击过了', '');
            }else{
                $data       = DB::table('activity')
                    ->select('attend')
                    ->where('activity_id',$activity_id)
                    ->get();
                if($data == true){
                    foreach ($data as $value){
                        $key  = $value ->attend;
                    }
                    $update   = DB::table('activity')
                        ->select('attend')
                        ->where('activity_id',$activity_id)
                        ->update(['attend'=>($key+1)]);
                    if($update   == true){
                        $insert  = DB::table('activitylike')->insert([
                            'user_activitylike'           => $user_id,
                            'activity_activitylike'        => $activity_id
                        ]);
                       // $likes  = array('like' =>$key);
                        return json::json('success','点击+1',json::value(1));
                    }else{
                        return json::json('failure','点击失败',json::value(2));
                    }
                }
            }}
    }

}